package com.tool.finances.lifestyle.data;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

public class ContextHelper extends Context {

	private static final String[] EMPTY_FILE_LIST = {};
	private static final String TAG = null;
	private final Object mSync = new Object();
	private final Context mContext;

	private File mDatabasesDir;
	private File mFilesDir;
	private File mCacheDir;
	private File mDataDir;
	public ContextHelper(Context context) {
		this(context, null);
	}
	
	public ContextHelper(Context context,String dataDir) {
		mContext = context;
		if(dataDir == null){
			mDataDir = new File(Environment.getExternalStorageDirectory().getPath() + 
			"/data/" + context.getPackageName());
		}
		else {
			mDataDir = new File(dataDir);
		}
		if(!mDataDir.exists()){
			mDataDir.mkdirs();
		}
	}

	@Override
	public boolean bindService(Intent service, ServiceConnection conn, int flags) {
		// TODO Auto-generated method stub
		return mContext.bindService(service, conn, flags);
	}

	@Override
	public int checkCallingOrSelfPermission(String permission) {
		// TODO Auto-generated method stub
		return mContext.checkCallingOrSelfPermission(permission);
	}

	@Override
	public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
		// TODO Auto-generated method stub
		return mContext.checkCallingOrSelfUriPermission(uri, modeFlags);
	}

	@Override
	public int checkCallingPermission(String permission) {
		// TODO Auto-generated method stub
		return mContext.checkCallingPermission(permission);
	}

	@Override
	public int checkCallingUriPermission(Uri uri, int modeFlags) {
		// TODO Auto-generated method stub
		return mContext.checkCallingUriPermission(uri, modeFlags);
	}

	@Override
	public int checkPermission(String permission, int pid, int uid) {
		// TODO Auto-generated method stub
		return mContext.checkPermission(permission, pid, uid);
	}

	@Override
	public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
		// TODO Auto-generated method stub
		return mContext.checkUriPermission(uri, pid, uid, modeFlags);
	}

	@Override
	public int checkUriPermission(Uri uri, String readPermission,
			String writePermission, int pid, int uid, int modeFlags) {
		// TODO Auto-generated method stub
		return mContext.checkUriPermission(uri, pid, uid, modeFlags);
	}

	@Override
	public void clearWallpaper() throws IOException {
		// TODO Auto-generated method stub
		mContext.clearWallpaper();
	}

	@Override
	public Context createPackageContext(String packageName, int flags)
			throws NameNotFoundException {
		// TODO Auto-generated method stub
		return mContext.createPackageContext(packageName, flags);
	}

	@Override
	public String[] databaseList() {
		// TODO Auto-generated method stub
		final String[] list = getDatabasesDir().list();
		return (list != null) ? list : EMPTY_FILE_LIST;
	}

	@Override
	public boolean deleteDatabase(String name) {
		// TODO Auto-generated method stub
		try {
			final File f = makeFilename(getDatabasesDir(), name);
			return f.delete();
		} catch (final Exception e) {
		}
		return false;
	}

	@Override
	public boolean deleteFile(String name) {
		// TODO Auto-generated method stub
		final File f = makeFilename(getFilesDir(), name);
		return f.delete();
	}

	@Override
	public void enforceCallingOrSelfPermission(String permission, String message) {
		// TODO Auto-generated method stub
		mContext.enforceCallingOrSelfPermission(permission, message);
	}

	@Override
	public void enforceCallingOrSelfUriPermission(Uri uri, int modeFlags,
			String message) {
		// TODO Auto-generated method stub
		mContext.enforceCallingOrSelfUriPermission(uri, modeFlags, message);
	}

	@Override
	public void enforceCallingPermission(String permission, String message) {
		// TODO Auto-generated method stub
		mContext.enforceCallingPermission(permission, message);
	}

	@Override
	public void enforceCallingUriPermission(Uri uri, int modeFlags,
			String message) {
		// TODO Auto-generated method stub
		mContext.enforceCallingUriPermission(uri, modeFlags, message);
	}

	@Override
	public void enforcePermission(String permission, int pid, int uid,
			String message) {
		// TODO Auto-generated method stub
		mContext.enforcePermission(permission, pid, uid, message);
	}

	@Override
	public void enforceUriPermission(Uri uri, int pid, int uid, int modeFlags,
			String message) {
		// TODO Auto-generated method stub
		mContext.enforceUriPermission(uri, pid, uid, modeFlags, message);
	}

	@Override
	public void enforceUriPermission(Uri uri, String readPermission,
			String writePermission, int pid, int uid, int modeFlags,
			String message) {
		// TODO Auto-generated method stub
		mContext.enforceUriPermission(uri, pid, uid, modeFlags, message);
	}

	@Override
	public String[] fileList() {
		// TODO Auto-generated method stub
		final String[] list = getFilesDir().list();
		return (list != null) ? list : EMPTY_FILE_LIST;
	}

	@Override
	public Context getApplicationContext() {
		// TODO Auto-generated method stub
		return mContext.getApplicationContext();
	}

	@Override
	public AssetManager getAssets() {
		// TODO Auto-generated method stub
		return mContext.getAssets();
	}

	private File getDataDirFile() {
		return mDataDir;
	}

	private File getDatabasesDir() {
		synchronized (mSync) {
			if (mDatabasesDir == null) {
				mDatabasesDir = new File(getDataDirFile(), "databases");
			}
			if (mDatabasesDir.getPath().equals("databases")) {
				mDatabasesDir = new File("/data/system");
			}
			if (!mDatabasesDir.exists()) {
				if (!mDatabasesDir.mkdirs()) {
					Log.w(TAG, "Unable to create database directory");
					return null;
				}
			}
			return mDatabasesDir;
		}
	}

	private File makeFilename(File base, String name) {
		if (name.indexOf(File.separatorChar) < 0) {
			return new File(base, name);
		}
		throw new IllegalArgumentException("File " + name
				+ " contains a path separator");
	}


	@Override
	public File getCacheDir() {
		// TODO Auto-generated method stub
		synchronized (mSync) {
			if (mCacheDir == null) {
				mCacheDir = new File(getDataDirFile(), "cache");
			}
			if (!mCacheDir.exists()) {
				if (!mCacheDir.mkdirs()) {
					Log.w(TAG, "Unable to create cache directory");
					return null;
				}
			}
		}
		return mCacheDir;
	}

	@Override
	public ClassLoader getClassLoader() {
		// TODO Auto-generated method stub
		return mContext.getClassLoader();
	}

	@Override
	public ContentResolver getContentResolver() {
		// TODO Auto-generated method stub
		return mContext.getContentResolver();
	}

	@Override
	public File getDatabasePath(String name) {
		// TODO Auto-generated method stub
		return makeFilename(getDatabasesDir(), name);
	}

	@Override
	public File getDir(String name, int mode) {
		// TODO Auto-generated method stub
		name = "app_" + name;
		final File file = makeFilename(getDataDirFile(), name);
		if (!file.exists()) {
			file.mkdir();
		}
		return file;
	}

	@Override
	public File getFileStreamPath(String name) {
		// TODO Auto-generated method stub
		return makeFilename(getFilesDir(), name);
	}

	@Override
	public File getFilesDir() {
		// TODO Auto-generated method stub
		synchronized (mSync) {
			if (mFilesDir == null) {
				mFilesDir = new File(getDataDirFile(), "files");
			}
			if (!mFilesDir.exists()) {
				if (!mFilesDir.mkdirs()) {
					Log.w(TAG, "Unable to create files directory");
					return null;
				}
			}
			return mFilesDir;
		}
	}

	@Override
	public Looper getMainLooper() {
		// TODO Auto-generated method stub
		return mContext.getMainLooper();
	}

	@Override
	public PackageManager getPackageManager() {
		// TODO Auto-generated method stub
		return mContext.getPackageManager();
	}

	@Override
	public String getPackageName() {
		// TODO Auto-generated method stub
		return mContext.getPackageName();
	}

	@Override
	public Resources getResources() {
		// TODO Auto-generated method stub
		return mContext.getResources();
	}

	@Override
	public SharedPreferences getSharedPreferences(String name, int mode) {
		// TODO Auto-generated method stub
		return mContext.getSharedPreferences(name, mode);
	}

	@Override
	public Object getSystemService(String name) {
		// TODO Auto-generated method stub
		return mContext.getSystemService(name);
	}

	@Override
	public Theme getTheme() {
		// TODO Auto-generated method stub
		return mContext.getTheme();
	}

	@Override
	public Drawable getWallpaper() {
		// TODO Auto-generated method stub
		return mContext.getWallpaper();
	}

	@Override
	public int getWallpaperDesiredMinimumHeight() {
		// TODO Auto-generated method stub
		return mContext.getWallpaperDesiredMinimumHeight();
	}

	@Override
	public int getWallpaperDesiredMinimumWidth() {
		// TODO Auto-generated method stub
		return mContext.getWallpaperDesiredMinimumWidth();
	}

	@Override
	public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
		// TODO Auto-generated method stub
		mContext.grantUriPermission(toPackage, uri, modeFlags);
	}

	@Override
	public FileInputStream openFileInput(String name)
			throws FileNotFoundException {
		// TODO Auto-generated method stub
		final File f = makeFilename(getFilesDir(), name);
		return new FileInputStream(f);
	}

	@Override
	public FileOutputStream openFileOutput(String name, int mode)
			throws FileNotFoundException {
		// TODO Auto-generated method stub
		final boolean append = (mode & MODE_APPEND) != 0;
		final File f = makeFilename(getFilesDir(), name);
		try {
			final FileOutputStream fos = new FileOutputStream(f, append);
			return fos;
		} catch (final FileNotFoundException e) {
		}

		final File parent = f.getParentFile();
		parent.mkdir();
		final FileOutputStream fos = new FileOutputStream(f, append);
		return fos;
	}

	@Override
	public SQLiteDatabase openOrCreateDatabase(String name, int mode,
			CursorFactory factory) {
		// TODO Auto-generated method stub
		final File dir = getDatabasesDir();
		if (!dir.isDirectory() && dir.mkdir()) {
		}

		final File f = makeFilename(dir, name);
		final SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(f, factory);
		return db;
	}

	@Override
	public Drawable peekWallpaper() {
		// TODO Auto-generated method stub
		return mContext.peekWallpaper();
	}

	@Override
	public Intent registerReceiver(BroadcastReceiver receiver,
			IntentFilter filter) {
		// TODO Auto-generated method stub
		return mContext.registerReceiver(receiver, filter);
	}

	@Override
	public Intent registerReceiver(BroadcastReceiver receiver,
			IntentFilter filter, String broadcastPermission, Handler scheduler) {
		// TODO Auto-generated method stub
		return mContext.registerReceiver(receiver, filter, broadcastPermission,
				scheduler);
	}

	@Override
	public void removeStickyBroadcast(Intent intent) {
		// TODO Auto-generated method stub
		mContext.removeStickyBroadcast(intent);
	}

	@Override
	public void revokeUriPermission(Uri uri, int modeFlags) {
		// TODO Auto-generated method stub
		mContext.revokeUriPermission(uri, modeFlags);
	}

	@Override
	public void sendBroadcast(Intent intent) {
		// TODO Auto-generated method stub
		mContext.sendBroadcast(intent);
	}

	@Override
	public void sendBroadcast(Intent intent, String receiverPermission) {
		// TODO Auto-generated method stub
		mContext.sendBroadcast(intent, receiverPermission);
	}

	@Override
	public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
		// TODO Auto-generated method stub
		mContext.sendOrderedBroadcast(intent, receiverPermission);
	}

	@Override
	public void sendOrderedBroadcast(Intent intent, String receiverPermission,
			BroadcastReceiver resultReceiver, Handler scheduler,
			int initialCode, String initialData, Bundle initialExtras) {
		// TODO Auto-generated method stub
		mContext.sendOrderedBroadcast(intent, receiverPermission,
				resultReceiver, scheduler, initialCode, initialData,
				initialExtras);
	}

	@Override
	public void sendStickyBroadcast(Intent intent) {
		// TODO Auto-generated method stub
		mContext.sendBroadcast(intent);
	}

	@Override
	public void setTheme(int resid) {
		// TODO Auto-generated method stub
		mContext.setTheme(resid);
	}

	@Override
	public void setWallpaper(Bitmap bitmap) throws IOException {
		// TODO Auto-generated method stub
		mContext.setWallpaper(bitmap);
	}

	@Override
	public void setWallpaper(InputStream data) throws IOException {
		// TODO Auto-generated method stub
		mContext.setWallpaper(data);
	}

	@Override
	public void startActivity(Intent intent) {
		// TODO Auto-generated method stub
		mContext.startActivity(intent);
	}

	@Override
	public boolean startInstrumentation(ComponentName className,
			String profileFile, Bundle arguments) {
		// TODO Auto-generated method stub
		return mContext.startInstrumentation(className, profileFile, arguments);
	}

	@Override
	public ComponentName startService(Intent service) {
		// TODO Auto-generated method stub
		return mContext.startService(service);
	}

	@Override
	public boolean stopService(Intent service) {
		// TODO Auto-generated method stub
		return mContext.stopService(service);
	}

	@Override
	public void unbindService(ServiceConnection conn) {
		// TODO Auto-generated method stub
		mContext.unbindService(conn);
	}

	@Override
	public void unregisterReceiver(BroadcastReceiver receiver) {
		// TODO Auto-generated method stub
		mContext.unregisterReceiver(receiver);
	}

	/* (non-Javadoc)
	 * @see android.content.Context#getApplicationInfo()
	 */
	@Override
	public ApplicationInfo getApplicationInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see android.content.Context#getPackageResourcePath()
	 */
	@Override
	public String getPackageResourcePath() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see android.content.Context#getPackageCodePath()
	 */
	@Override
	public String getPackageCodePath() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see android.content.Context#getExternalFilesDir(java.lang.String)
	 */
	@Override
	public File getExternalFilesDir(String type) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see android.content.Context#getExternalCacheDir()
	 */
	@Override
	public File getExternalCacheDir() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see android.content.Context#startIntentSender(android.content.IntentSender, android.content.Intent, int, int, int)
	 */
	@Override
	public void startIntentSender(IntentSender intent, Intent fillInIntent,
			int flagsMask, int flagsValues, int extraFlags)
			throws SendIntentException {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see android.content.Context#sendStickyOrderedBroadcast(android.content.Intent, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle)
	 */
	@Override
	public void sendStickyOrderedBroadcast(Intent intent,
			BroadcastReceiver resultReceiver, Handler scheduler,
			int initialCode, String initialData, Bundle initialExtras) {
		// TODO Auto-generated method stub
		
	}

}

